/*
 * Copyright (c) 2010. raoqn
 */

package com.yfeng.goodjuhome.service.impl;

import com.yfeng.common.dao.IDao;
import com.yfeng.exception.ServiceException;
import com.yfeng.goodjuhome.model.Product;
import com.yfeng.goodjuhome.service.IProductService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * www.goodjuhome.com
 * Created by IntelliJ IDEA.
 * User: rqn
 * Date: 2010-8-14
 * Time: 17:24:07
 */
@Service("productService")
public class ProductServiceImpl implements IProductService {

     private final Log log = LogFactory.getLog(this.getClass());

    @Autowired
	public IDao baseDao;

    public int addProduct(Product product) throws ServiceException {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public int[] deleteProduct(String[] ids) throws ServiceException {
        return new int[0];  //To change body of implemented methods use File | Settings | File Templates.
    }

    public int updateProduct(Product product) throws ServiceException {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Product getProduct(String id) throws ServiceException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List queryProductList() throws ServiceException {
        List ret = new ArrayList();
       try
       {
           List<Map> result = baseDao.query("select t.*,t2.path from products t left join uploads t2 on t.id=t2.pid and t2.type=product");

           return result;
       }
       catch(Exception e)
       {
          log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);

       }

    }

    public List queryProductList(String limit) throws ServiceException {
        try
       {
           List result=null;
           if(StringUtils.isBlank(limit))
              result = baseDao.query("select * from products limit 20");
           else
              result =  baseDao.query("select * from products limit "+limit);
           return result;
       }
       catch(Exception e)
       {
          log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);

       }
    }

    public List queryProductListByMidType(String typeid) throws ServiceException {
        try
       {
           List result = baseDao.query("select * from products where mid_type='"+typeid+"'");

           return result;
       }
       catch(Exception e)
       {
          log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);

       }
    }

    public List queryProductListByMidType(String typeid,String limit) throws ServiceException {
        try
       {
            List result=null;
           if(StringUtils.isBlank(limit))
              result = baseDao.query("select * from products where mid_type='"+typeid+"' limit 20");
           else
              result =  baseDao.query("select * from products where mid_type='"+typeid+"' limit "+limit);
           return result;

       }
       catch(Exception e)
       {
          log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);

       }
    }



    public List queryProductListByBigType(String typeid) throws ServiceException {
        try
       {
           List result = baseDao.query("select * from products where big_type='"+typeid+"'");

           return result;
       }
       catch(Exception e)
       {
          log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);

       }
    }

    public List queryProductListByBigType(String typeid,String limit) throws ServiceException {
        try
       {
            List result=null;
           if(StringUtils.isBlank(limit))
              result = baseDao.query("select * from products t, where big_type='"+typeid+"' limit 20");
           else
              result =  baseDao.query("select * from products where big_type='"+typeid+"' limit "+limit);
           return result;

       }
       catch(Exception e)
       {
          log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);

       }
    }

    public List queryProductListByUid(String uid) throws ServiceException {
        try
       {
           List result = baseDao.query("select * from products where uid='"+uid+"'");

           return result;
       }
       catch(Exception e)
       {
          log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);

       }
    }

    public List queryProductListByUid(String uid, String limit) throws ServiceException {
        try
       {
           List result=null;
           if(StringUtils.isBlank(limit))
              result = baseDao.query("select * from products where uid='"+uid+"' limit 20");
           else
              result =  baseDao.query("select * from products where uid='"+uid+"' limit "+limit);
           return result;
       }
       catch(Exception e)
       {
          log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);

       }
    }

    public boolean isExist(String name) throws ServiceException {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
